//
//  TopicViewController.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TopicViewController.h"
#import "LYPrefixHeader.pch"
@interface TopicViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation TopicViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor redColor];
    // 标题
    self.navigationItem.title = @"问吧";
    
    self.navigationController.navigationBar.alpha = 1;
    
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    // 导航栏上按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    // 数据处理
    [self data];
    
    [_tableView registerClass:[TopicCell class] forCellReuseIdentifier:@"cell"];
    // 刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
   

}
// 刷新
- (void)headerData
{
    NSString *str = @"http://c.m.163.com/newstopic/list/expert/0-10.html";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"expertList"];
        self.dataArr = [TopicModel modelHandleWithArray:arr];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];

}
// 加载
- (void)footerData
{
    static NSInteger t = 20;
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/newstopic/list/expert/0-%ld.html", t];
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        self.dataArr = [TopicModel modelHandleWithArray:result[@"data"][@"expertList"]];
        [self.tableView reloadData];
    }];
    t += 20;
    
}
- (void)data
{
    NSString *str = @"http://c.m.163.com/newstopic/list/expert/0-10.html";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"expertList"];
        self.dataArr = [TopicModel modelHandleWithArray:arr];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TopicModel *m = _dataArr[indexPath.row];
    [cell.backpicture  sd_setImageWithURL:[NSURL URLWithString:m.picurl]];
    [cell.headpicture sd_setImageWithURL:[NSURL URLWithString:m.headpicurl]];
    cell.name.text = m.name;
    cell.title.text = m.alias;
    // 拼接字符串
    cell.concernCount.text = [[m.concernCount stringValue] stringByAppendingString:@"关注 | "];
    cell.classification.text = m.classification;

    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 305;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTopicVC *detail = [[DetailTopicVC alloc] init];
    TopicModel *m = _dataArr[indexPath.row];
    detail.str = m.expertId;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
